#pragma once

#include "Mutex.hpp"
#include "Cond.hpp"
#include "Thread.hpp"
#include "Log.hpp"
#include <vector>
#include <queue>
#include <iostream>



namespace ThreadPoolModule
{
    using namespace MutexModule;
    using namespace CondModule;
    using namespace ThreadModule;
    using namespace LogModule;

    template <class T>
    class ThreadPool
    {
        void WakeUpOneThread()
        {
            _cond.Signal();
            //LOG(LogLevel::INFO) << "唤醒一个休眠线程";
        }

        void WakeUpAllThread()
        {
            _cond.Broadcast();
            //LOG(LogLevel::INFO) << "唤醒所有休眠线程";
        }

        ThreadPool()
            : _num(5),
            _isrunning(false),
            _sleepnum(0)
        {
            for (int i = 0; i < _num; i++)
            {
                _thread.emplace_back(
                    [this]() {
                        HandlerTask();//想想vector<string> v  v.push_back,v.emplace 将什么数据传入了进去
                    });
            }
        }

        void Start()
        {
            if(_isrunning)
                return;
            _isrunning = true;

            for(auto& t:_thread)
            {
                t.Start();
            }
        }

        ThreadPool(const ThreadPool<T>& ) = delete;
        ThreadPool<T>& operator=(const ThreadPool<T>&) = delete;

    public:
        static ThreadPool<T> *GetInstStance()
        {
            if(Inst == nullptr)
            {
                LockGuard lockguard(_lock);

                LOG(LogLevel::INFO) << "获取单例";
                if(Inst == nullptr)
                {
                    LOG(LogLevel::INFO) << "首次使用单例";
                    Inst = new ThreadPool();
                    Inst->Start();
                }
            }
            return Inst;
        }

        

        void Stop()
        {
            if(!_isrunning)
                return;
            _isrunning = false;
            if(_sleepnum > 0)
                WakeUpAllThread();
        }

        bool Equeue(const T& in)
        {
            if(_isrunning)
            {
                _task.push(in);
                if(_thread.size() - _sleepnum == 0)
                    WakeUpOneThread();
                return true;
            }
            return false;
        }

        void Join()
        {
            for(auto& thread : _thread)
            {
                thread.Join();
            }
        }
        void HandlerTask()
        {
            char name[128];
            pthread_getname_np(pthread_self(), name, sizeof(name));
            while (true)
            {
                T t;
                LockGuard lockguard(_mutex);
                {
                    while (_task.empty()&&_isrunning)
                    {
                        LOG(LogLevel::INFO)  << name << "睡眠";
                        _sleepnum++;
                        _cond.Wait(_mutex);
                        LOG(LogLevel::INFO)  << name << "被唤醒";
                        _sleepnum--;
                    }

                    if(_task.empty()&&!_isrunning)
                        break;
                    t = _task.front();
                    _task.pop();
                    LOG(LogLevel::INFO)  << name << "去执行DownLoad任务";
                    t();
                }
            }
        }
        
    private:
        int _num;
        std::vector<Thread> _thread;
        std::queue<T> _task;
        Cond _cond;
        Mutex _mutex;
        bool _isrunning;
        int _sleepnum;
        static ThreadPool<T>* Inst;
        static Mutex _lock;
    };

    template<class T>
    ThreadPool<T>* ThreadPool<T>::Inst = nullptr;
    
    template<class T>
    Mutex ThreadPool<T>::_lock;
}

